package com.changfen.controller;

import com.changfen.Result;
import com.changfen.domain.Order;
import com.changfen.dto.OrderDto;
import com.changfen.feign.OrderClient;
import com.changfen.service.OrderService;
import com.changfen.service.TccOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author changfeng.chen
 * @date 2021/5/13
 * Email: changfeng.chen@things-matrix.com
 */
@RestController
@Slf4j
public class OrderController implements OrderClient {
    @Resource
    private OrderService orderService;

    @Resource
    private TccOrderService tccOrderService;

    @Override
    @GlobalTransactional(timeoutMills = 30000,name = "gts-seata-example")
    public Result<Void> addOrder(OrderDto orderDto) {
        Order order = new Order();
        BeanUtils.copyProperties(orderDto,order);
        orderService.saveAndFlush(order);
        int i = 10 / 0;
        return Result.ok();
    }

    @Override
    public Result<Void> addTccOrder(OrderDto orderDto) {
        Order order = new Order();
        BeanUtils.copyProperties(orderDto, order);
        orderService.saveAndFlush(order);
        tccOrderService.createAddOrder(null,order);
        return Result.ok();
    }

    @Override
    public Result<Void> updateOrder(OrderDto orderDto) {
        return null;
    }


}
